﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace RpgEditor
{
    public class Event
    {

        const SByte IF = 0;
        const SByte IF_NOT = 1;
        const SByte OR = 2;
        static String[] TYPE_STR = { "IF", "IF_NOT", "OR" };
        // 参数类型(目前支持七种常用类型,浮点数暂不考虑)
        const SByte SByte = 0;
        const SByte INT = 1;
        const SByte BOOLEAN = 2;
        const SByte CHAR = 3;
        const SByte SHORT = 4;
        const SByte LONG = 5;
        const SByte STRING = 6;

        const SByte IF_MOVE_TO = 0; // 如果移动到某个位置
        const SByte CAN_TALK_TO = 1;
        const SByte SHOW_ALERT_MSG = 2;
        const SByte CHECK_MISSION_STATE = 3;
        const SByte IS_MEMBER = 4;
        const SByte CHECK_HP = 5;
        const SByte ALL_CLEAR = 6;
        const SByte CHECK_DIRECTION = 7;
        const SByte CHECK_MISSION_STATE_LESS_THAN = 8;
        const SByte TR_BOX = 9;
        const SByte BY_SIDE = 10;
        const SByte CHK_ITEM = 11;
        const SByte CAN_FIGHT_TO = 12;
        const SByte HAVE_ITEM = 13;
        const SByte IS_EQUIPED = 14;
        const SByte IS_TEAM_NUM = 15;
        const SByte PAY_POINT = 16;
        const SByte HAVE_SKILL = 17;
        const SByte PAY_CHK = 18;
        const SByte IF_SET_MISSION_STATE = 19;
        const SByte CHECK_MISSION_STATE_GREATER_THAN = 20;

        // 各种判断中各个传入参数的类型
        static SByte[][] CONDITION_PARA_TYPE = {
		new SByte[] { SByte, SByte },  // is_move_to
		new SByte[] { SByte }, //can_talk_to
		new SByte[] { STRING }, // showAlertMsg
		new SByte[] { SByte,SByte }, // check_mission_state
		new SByte[] { SByte }, // is_member
		new SByte[] { SByte,SByte },  // CHECK_HP
		new SByte[] { SByte },       // ALL_CLEAR
		new SByte[] { SByte,SByte },  // CHECK_DIRECTION
		new SByte[] { SByte,SByte },  // check mission state less than
		new SByte[] { SByte,SByte,SByte,SByte,SHORT}, // tr box
		new SByte[] { SByte,SByte },    // BY_SIDE
		new SByte[] { SByte,SByte },  // check_item
		new SByte[] { SByte },  // can fight to
		new SByte[] { SByte,SByte }, // HAVE_ITEM
		new SByte[] { SByte,SByte }, // is equiped
		new SByte[] { SByte },    // IS_TEAM_NUM
		new SByte[] { STRING,SByte }, // PAY_POINT
		new SByte[] { SByte,SByte}, // HAVE_SKILL
		new SByte[] {},  // PAY_CHK
		new SByte[] { SByte,SByte },  // IF_SET_MISSION_STATE
		new SByte[] { SByte,SByte },
	};
        static String[] CONDITION_STR = { 
"IF_MOVE_TO",
"CAN_TALK_TO",
"SHOW_ALERT_MSG",
"CHECK_MISSION_STATE",
"IS_MEMBER",
"CHECK_HP",
"ALL_CLEAR",
"CHECK_DIRECTION",
"CHECK_MISSION_STATE_LESS_THAN",
"TR_BOX",
"BY_SIDE",
"CHK_ITEM",
"CAN_FIGHT_TO",
"HAVE_ITEM",
"IS_EQUIPED",
"IS_TEAM_NUM",
"PAY_POINT",
"HAVE_SKILL",
"PAY_CHK",
"IF_SET_MISSION_STATE",
"CHECK_MISSION_STATE_GREATER_THAN"};
        static SByte SHOW_SYSTEM_MSG = 0;
        static SByte JUMP_MAP = 1;
        static SByte ADD_NPC = 2;
        static SByte CREATE_TALK = 3;
        static SByte END_TALK = 4;
        static SByte SET_GOODS = 5;
        static SByte SHOW_SHOP = 6;
        static SByte SET_MISSION_STATE = 7;
        static SByte INVITE_JOIN = 8;
        static SByte ADD_HP = 9;
        static SByte ADD_MP = 10;

        static SByte GAME_OVER = 11;
        static SByte WIN = 12;
        static SByte SHOW_MAP = 13;
        static SByte GAME_WAIT = 14;
        static SByte SHOW_FACE = 15;
        static SByte SET_DIRECTION = 16;
        static SByte MOVE_ROLE = 17;
        static SByte REMOVE_ROLE = 18;
        static SByte SET_ROLE_IN_SCREEN = 19;
        static SByte GOTO_WAR = 20;

        static SByte GET_ITEM = 21;
        static SByte GET_MONEY = 22;
        static SByte MOVE_ROLE_TO = 23;
        static SByte SET_MAP_NAME = 24;
        static SByte ADD_MONSTER = 25;
        static SByte SET_ROLE_NAME = 26;
        static SByte PLAY_SOUND = 27;
        static SByte STOP_SOUND = 28;
        static SByte LEAVE_TEAM = 29;
        static SByte SHOW_MENU = 30;

        static SByte CHECK_MONEY = 31;
        static SByte END_WAR = 32;
        static SByte ADD_SKILL = 33;
        static SByte GET_RANDOM = 34;
        static SByte STAND_STILL = 35;
        static SByte SAVE = 36;
        static SByte SCEAN_MSG = 37;
        static SByte BACK_TO_TITLE = 38;
        static SByte SET_LEVEL = 39;
        static SByte SYNC_MOVE_ROLE = 40;
        static SByte START_SYNC_MOVE = 41;
        static SByte SET_ROLE_IN_POSITION = 42;
        static String[] FUNCTION_STR = { 
"SHOW_SYSTEM_MSG",
"JUMP_MAP",
"ADD_NPC",
"CREATE_TALK",
"END_TALK",
"SET_GOODS",
"SHOW_SHOP",
"SET_MISSION_STATE",
"INVITE_JOIN",
"ADD_HP",
"ADD_MP",

"GAME_OVER",
"WIN",
"SHOW_MAP",
"GAME_WAIT",
"SHOW_FACE",
"SET_DIRECTION",
"MOVE_ROLE",
"REMOVE_ROLE",
"SET_ROLE_IN_SCREEN",
"GOTO_WAR",
"GET_ITEM",
"GET_MONEY",
"MOVE_ROLE_TO",
"SET_MAP_NAME",
"ADD_MONSTER ",
"SET_ROLE_NAME",
"PLAY_SOUND",
"STOP_SOUND",
"LEAVE_TEAM",
"SHOW_MENU",

"CHECK_MONEY",
"END_WAR",
"ADD_SKILL",
"GET_RANDOM",
"STAND_STILL",
"SAVE",
"SCEAN_MSG",
"BACK_TO_TITLE",
"SET_LEVEL",
"SYNC_MOVE_ROLE",
"START_SYNC_MOVE",
"SET_ROLE_IN_POSITION",
};
        static SByte[][] FUNCTION_PARA_TYPE = { // 各种函数中各个传入的参数类型
		new SByte[] { STRING },  // show system msg
		new SByte[] { SByte,SByte,SByte,SByte }, // jump map
		new SByte[] { STRING,SHORT,SByte,SByte,SByte,SByte,BOOLEAN,SByte},  // add npc
		new SByte[] { SByte,STRING,SByte },  // CREATE_TALK
		new SByte[] {},  // end talk
		new SByte[] { SByte,SByte },  // set goods
		new SByte[] {}, // show shop
		new SByte[] { SByte,SByte },  // SET_MISSION_STATE
		new SByte[] { STRING,SByte }, // invite join
		new SByte[] { SByte },  // add hp
		new SByte[] { SByte },  // add mp
		new SByte[] {},     // GAME_OVER
		new SByte[] {},     // WIN
		new SByte[] { BOOLEAN },     // SHOW_MAP
		new SByte[] { LONG },  // game wait
		new SByte[] { SByte,SByte }, // show face
		new SByte[] { SByte,SByte },   // SET_DIRECTION
		new SByte[] { SByte,BOOLEAN,SByte,SByte,SByte }, // MOVE_ROLE 
		new SByte[] { SByte },  // REMOVE_ROLE
		new SByte[] { SByte,SByte },        // SET_ROLE_IN_SCREEN
		new SByte[] { SHORT },  // GOTO_WAR
		new SByte[] { SByte,SByte },   // GET_ITEM
		new SByte[] { INT }, // GET_MONEY
		new SByte[] { SByte,BOOLEAN,SByte,SByte,SByte },  // MOVE_ROLE_TO
		new SByte[] { STRING }, // SET_MAP_NAME
		new SByte[] { STRING,SByte,SByte,SByte,SByte,SByte,BOOLEAN,SByte }, // ADD_MONSTER
		new SByte[] { SByte,STRING }, // set role name
		new SByte[] { SByte,SByte },  // play midi
		new SByte[] { SByte },  // stop midi
		new SByte[] { SByte },  // leave team
		new SByte[] { SByte,SByte,STRING,STRING },  // SHOW_MENU
		new SByte[] { SHORT,SByte,SByte },  // CHECK_MONEY
		new SByte[] {}, // end war
		new SByte[] { SByte,SByte,SByte }, // ADD_SKILL
		new SByte[] { SByte,SByte }, // GET_RANDOM
		new SByte[] { SByte,SByte }, // stand still
		new SByte[] {},  // SAVE
		new SByte[] { STRING },  // SCEAN_MSG
		new SByte[] { BACK_TO_TITLE }, // BACK_TO_TITLE
		new SByte[] { SByte,SByte }, // SET_LEVEL
		new SByte[] { SByte,SByte,SByte,SByte,SByte },
		new SByte[] {  },
		new SByte[] { SByte,SByte,SByte }, // SET_ROLE_IN_POSITION
	};

        const SByte EVENT_INIT = 0; // 12.28: 初始化
        const SByte EVENT_INITIATIVE = 1; // 12.28: 主动
        const SByte EVENT_AUTO = 2; // 12.28: 自动

        // 将不同类型的事件分开保存有利于提高速度和方便管理
        public static Event[][] subEvent;

        public SByte[] condition; // 条件列表,保存的是条件的类型
        public SByte[] conditionType;
        public ArrayList[] conditionParam; // 参数列表,保存的是每一个条件需要传入的参数

        public SByte[] function; // 条件成立时需要执行的涵数类型列表
        public ArrayList[] functionParam; // 条件成立时需要执行的涵数的参数列表
        mainForm form;
        String fn = null;
        // 函数参数最多8个，以后如果增加函数，参数超过8个，这里要改
        public static TextBox[] funPara = new TextBox[8];

        public Event(mainForm f)
        {
            form = f;
            if (f != null)
            {
                f.comboBox1.Items.Clear();
                f.comboBox2.Items.Clear();
                for (int i = 0; i < CONDITION_STR.Length; i++)
                {
                    f.comboBox1.Items.Add(CONDITION_STR[i]);
                }
                for (int i = 0; i < TYPE_STR.Length; i++)
                {
                    f.comboBox2.Items.Add(TYPE_STR[i]);
                }
                for (int i = 0; i < FUNCTION_STR.Length; i++)
                {
                    f.comboBox3.Items.Add(FUNCTION_STR[i]);
                }
            }
        }
        public void setFn(String fn)
        {
            this.fn = fn;
            form.textBox12.Text = fn;
        }

        public String getFn()
        {
            return fn;
        }
        public void save(String fn)
        {
            FileStream fs1 = new FileStream(fn, FileMode.Create, FileAccess.Write);
            BinaryWriter dis = new BinaryWriter(fs1, System.Text.Encoding.Default);
            // 有多少个事件类型
            MyIO.writeByte(dis, (SByte)(subEvent.Length));

            for (int i = 0; i < subEvent.Length; i++)
            {
                // 该事件类型有多少事件
                if (subEvent[i] == null)
                {
                    MyIO.writeByte(dis, (SByte)(0));
                }
                else
                {
                    MyIO.writeByte(dis, (SByte)(subEvent[i].Length));

                    for (int j = 0; j < subEvent[i].Length; j++)
                    {
                        subEvent[i][j].save(dis);
                    }
                }
            }
            dis.Close();

        }
        public void save(BinaryWriter dis)
        {

            // 有多少个条件判断
            if (condition == null)
            {
                MyIO.writeByte(dis, (SByte)0);
            }
            else
            {
                MyIO.writeByte(dis, (SByte)(condition.Length));
                for (int i = 0; i < condition.Length; i++)
                { // 分别读出每个条件判断的类型和参数

                    MyIO.writeByte(dis, (SByte)(condition[i]));
                    MyIO.writeByte(dis, (SByte)(conditionType[i]));
                    int paraNum = CONDITION_PARA_TYPE[condition[i]].Length; // 取得该判断需要的参数数量
                    for (int j = 0; j < paraNum; j++)
                    { // 对每一个参数分别考虑,读出并保存
                        switch (CONDITION_PARA_TYPE[condition[i]][j])
                        {
                            case SByte:
                                MyIO.writeByte(dis, (SByte)(conditionParam[i][j]));
                                break;
                            case INT:
                                MyIO.writeInt32(dis, (int)(conditionParam[i][j]));
                                break;
                            case BOOLEAN:

                                MyIO.writeByte(dis, (SByte)(conditionParam[i][j]));
                                break;
                            case CHAR:
                                //java:
                                //Reads an input char and returns the char value.
                                //A Unicode char is made up of two bytes. Let a be the first byte read and b be the second byte. The value returned is: 
                                //(char)((a << 8) | (b & 0xff))

                                //conditionParam[i].Add(new Character(dis.readChar()));
                                MyIO.writeByte(dis, (SByte)(conditionParam[i][j]));
                                break;
                            case SHORT:
                                MyIO.writeInt16(dis, (short)(conditionParam[i][j]));
                                break;
                            case LONG:
                                MyIO.writeInt64(dis, (long)(conditionParam[i][j]));
                                break;
                            case STRING:
                                MyIO.writeString(dis, (String)(conditionParam[i][j]));
                                break;
                        }
                    }
                }
            }
            if (function == null)
            {
                MyIO.writeByte(dis, (SByte)(0));
            }
            else
            {
                MyIO.writeByte(dis, (SByte)(function.Length));
                for (int i = 0; i < function.Length; i++)
                {
                    MyIO.writeByte(dis, (SByte)(function[i]));
                    int paraNum = FUNCTION_PARA_TYPE[function[i]].Length;
                    for (int j = 0; j < paraNum; j++)
                    { // 对每一个参数分别考虑,读出并保存
                        switch (FUNCTION_PARA_TYPE[function[i]][j])
                        {
                            case SByte:
                                MyIO.writeByte(dis, (SByte)(functionParam[i][j]));
                                break;
                            case INT:
                                MyIO.writeInt32(dis, (int)(functionParam[i][j]));
                                break;
                            case BOOLEAN:
                                MyIO.writeByte(dis, (SByte)(functionParam[i][j]));
                                break;
                            case CHAR:
                                MyIO.writeByte(dis, (SByte)(functionParam[i][j]));
                                break;
                            case SHORT:
                                MyIO.writeInt16(dis, (short)(functionParam[i][j]));
                                break;
                            case LONG:
                                MyIO.writeInt64(dis, (long)(functionParam[i][j]));
                                break;
                            case STRING:
                                MyIO.writeString(dis, (String)(functionParam[i][j]));

                                break;
                        }
                    }
                }
            }
        }

        public void loadEvent(String fn)
        {

            FileStream fs1 = new FileStream(fn, FileMode.Open, FileAccess.Read);
            BinaryReader dis = new BinaryReader(fs1, System.Text.Encoding.Default);
            readAllEvent(dis);
            dis.Close();

        }

        public void readAllEvent(BinaryReader dis)
        {
            // 有多少个事件类型
            int num = MyIO.readByte(dis);
            subEvent = new Event[num][];
            for (int i = 0; i < num; i++)
            {
                // 该事件类型有多少事件
                int eventnum = MyIO.readByte(dis);
                subEvent[i] = new Event[eventnum];
                for (int j = 0; j < eventnum; j++)
                {
                    subEvent[i][j] = new Event(null);
                    subEvent[i][j].init(dis);
                }
            }


        }

        void init(BinaryReader dis)
        {

            // 有多少个条件判断
            int num = MyIO.readByte(dis);
            condition = new SByte[num];
            conditionType = new SByte[num];
            conditionParam = new ArrayList[num];

            for (int i = 0; i < num; i++)
            { // 分别读出每个条件判断的类型和参数
                conditionParam[i] = new ArrayList();
                condition[i] = MyIO.readByte(dis);
                conditionType[i] = MyIO.readByte(dis);
                int paraNum = CONDITION_PARA_TYPE[condition[i]].Length; // 取得该判断需要的参数数量
                for (int j = 0; j < paraNum; j++)
                { // 对每一个参数分别考虑,读出并保存
                    switch (CONDITION_PARA_TYPE[condition[i]][j])
                    {
                        case SByte:
                            conditionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case INT:
                            conditionParam[i].Add(MyIO.readInt32(dis));
                            break;
                        case BOOLEAN:

                            conditionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case CHAR:
                            //java:
                            //Reads an input char and returns the char value.
                            //A Unicode char is made up of two bytes. Let a be the first byte read and b be the second byte. The value returned is: 
                            //(char)((a << 8) | (b & 0xff))

                            //conditionParam[i].Add(new Character(dis.readChar()));
                            conditionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case SHORT:
                            conditionParam[i].Add(MyIO.readInt16(dis));
                            break;
                        case LONG:
                            conditionParam[i].Add(MyIO.readInt64(dis));
                            break;
                        case STRING:
                            conditionParam[i].Add(MyIO.readString(dis));
                            break;
                    }
                }
            }

            num = MyIO.readByte(dis);
            function = new SByte[num];
            functionParam = new ArrayList[num];

            for (int i = 0; i < num; i++)
            {
                functionParam[i] = new ArrayList();
                function[i] = MyIO.readByte(dis);
                int paraNum = FUNCTION_PARA_TYPE[function[i]].Length;
                for (int j = 0; j < paraNum; j++)
                { // 对每一个参数分别考虑,读出并保存
                    switch (FUNCTION_PARA_TYPE[function[i]][j])
                    {
                        case SByte:
                            functionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case INT:
                            functionParam[i].Add(MyIO.readInt32(dis));
                            break;
                        case BOOLEAN:
                            functionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case CHAR:
                            functionParam[i].Add(MyIO.readByte(dis));
                            break;
                        case SHORT:
                            functionParam[i].Add(MyIO.readInt16(dis));
                            break;
                        case LONG:
                            functionParam[i].Add(MyIO.readInt64(dis));
                            break;
                        case STRING:
                            String s = MyIO.readString(dis);
                            functionParam[i].Add(s);
                            break;
                    }
                }
            }
        }

        public void addSub(int index)
        {
            Event e = new Event(null);
            Event[] newe;
            if (subEvent[index] == null)
            {
                newe = new Event[1];
                newe[0] = e;
            }
            else
            {
                newe = new Event[subEvent[index].Length + 1];
                for (int i = 0; i < subEvent[index].Length; i++)
                {
                    newe[i] = subEvent[index][i];
                }
                newe[subEvent[index].Length] = e;
            }

            subEvent[index] = newe;
        }


        public void delSub(int i1,int i2)
        {
            Event[] e = subEvent[i1];
            if (i2 >= e.Length)
            {
                return;
            }
            Event[] newe = new Event[e.Length-1];
            int j = 0;
            for (int i = 0; i < e.Length; i++)
            {
                if (i != i2)
                {
                    newe[j] = e[i];
                    j++;
                }
            }

            subEvent[i1] = newe;
        }

        public void display()
        {
            int select = -1;
            select = form.listBox5.SelectedIndex;
            
            form.listBox5.Items.Clear();
            for (int i = 0; i < subEvent.Length; i++)
            {
                String s = "";
                switch (i)
                {
                    case EVENT_INIT: s = "EVENT_INIT"; break;
                    case EVENT_INITIATIVE: s = "EVENT_INITIATIVE"; break;
                    case EVENT_AUTO: s = "EVENT_AUTO"; break;
                    default: s = "EVENT_INIT"; break;
                }
                form.listBox5.Items.Add(s);

            }
            if (select < form.listBox5.Items.Count && select != -1)
                form.listBox5.SelectedIndex = select;
            else
                form.listBox5.SelectedIndex = 0;
            displaymode(form.listBox5.SelectedIndex);
        }

        public void displaymode(int mode)
        {
            if (mode < 0)
                mode = 0;
            int select = 0;
            if (form.listBox6.SelectedIndex != -1)
                select = form.listBox6.SelectedIndex;

            form.listBox6.Items.Clear();
            Event[] s = subEvent[mode];
            if (s != null)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    form.listBox6.Items.Add(i);
                }
                if (form.listBox6.Items.Count > 0)
                {
                    if (select < form.listBox6.Items.Count)
                    form.listBox6.SelectedIndex = select;
                    else
                    form.listBox6.SelectedIndex = 0;
                    displaysub(form.listBox5.SelectedIndex, form.listBox6.SelectedIndex);
                }
            }

        }
        public void movesub(int mode, int index, int dir)
        {
            Event[] s = subEvent[mode];
            int newindex = -1;
            if (dir == 0)
            {
                newindex = index - 1;
            }
            else
            {
                newindex = index + 1;
            }
            if (newindex >= 0 && newindex < form.listBox6.Items.Count)
            {
                Event temp = s[index];
                s[index] = s[newindex];
                s[newindex] = temp;
            }
        }

        public void movesub(int i1, int i2,int i3, int dir)
        {
            Event s = subEvent[i1][i2];

            form.comboBox3.SelectedIndex = s.function[i3];
       



            int newindex = -1;
            if (dir == 0)
            {
                newindex = i3 - 1;
            }
            else
            {
                newindex = i3 + 1;
            }
            if ( newindex >= 0 && newindex < s.function.Length)
            {
                sbyte temp = s.function[i3];
                s.function[i3] = s.function[newindex];
                s.function[newindex] = temp;

                ArrayList tt = s.functionParam[i3];
                s.functionParam[i3] = s.functionParam[newindex];
                s.functionParam[newindex] = tt;
            }
        }

        public void movesubpos(int i1, int i2, int i3, int pos)
        {
            Event s = subEvent[i1][i2];

           // form.comboBox3.SelectedIndex = s.function[i3];
           



            int newindex = pos;
            
            if ( newindex >= 0 && newindex < s.function.Length)
            {
                ArrayList temp = new ArrayList();
                for (int i = 0; i < s.function.Length; i++)
                {
                    temp.Add(s.function[i]);
                }
                SByte st = s.function[i3];
                temp.RemoveAt(i3);
                temp.Insert(newindex, st);
                for (int i = 0; i < s.function.Length; i++)
                {
                    s.function[i] = (SByte)temp[i];
                }

                temp = new ArrayList();
                for (int i = 0; i < s.functionParam.Length; i++)
                {
                    temp.Add(s.functionParam[i]);
                }
                ArrayList sl = s.functionParam[i3];
                temp.RemoveAt(i3);
                temp.Insert(newindex, sl);
                for (int i = 0; i < s.functionParam.Length; i++)
                {
                    s.functionParam[i] = (ArrayList)temp[i];
                }
            }
        }

        public void displaysub(int index1, int index2)
        {
            Event s = subEvent[index1][index2];
            form.textBox13.Lines = s.toString();

            form.listBox7.Items.Clear();
            if (s.condition != null)
            {
                for (int i = 0; i < s.condition.Length; i++)
                {
                    form.listBox7.Items.Add(i);
                }
            }
            form.listBox8.Items.Clear();
            if (s.function != null)
            {
                for (int i = 0; i < s.function.Length; i++)
                {
                    form.listBox8.Items.Add(i);
                }
            }

        }
        public void displaycondition(int i1, int i2, int i3)
        {
            Event s = subEvent[i1][i2];
            form.comboBox1.SelectedIndex = s.condition[i3];
            form.comboBox2.SelectedIndex = s.conditionType[i3];
            ArrayList l = s.conditionParam[i3];
            form.textBox14.Text = "";
            for (int i = 0; i < l.Count; i++)
            {
                form.textBox14.Text = form.textBox14.Text + l[i] + ",";
            }
        }
        public void displayfun(int i1, int i2, int i3)
        {
            Event s = subEvent[i1][i2];
            form.comboBox3.SelectedIndex = s.function[i3];

            ArrayList l = s.functionParam[i3];

            for (int i = 0; i < l.Count; i++)
            {
                funPara[i].Text = ""+l[i];
            }
        }
        public bool addfunction(int i1, int i2, int c)
        {
            Event s = subEvent[i1][i2];
            SByte[] newe;
            if (s.function == null)
            {
                newe = new SByte[1];
                newe[0] = (SByte)c;
            }
            else
            {
                newe = new SByte[s.function.Length + 1];
                for (int i = 0; i < s.function.Length; i++)
                {
                    newe[i] = s.function[i];
                }
                newe[s.function.Length] = (SByte)c;
            }
            s.function = newe;            

            ArrayList[] tl;
            if (s.functionParam == null)
            {
                tl = new ArrayList[1];
                tl[0] = new ArrayList();
            }
            else
            {
                tl = new ArrayList[s.functionParam.Length + 1];
                for (int i = 0; i < s.functionParam.Length; i++)
                {
                    tl[i] = s.functionParam[i];
                }
                tl[s.functionParam.Length] = new ArrayList();
            }
            s.functionParam = tl;


            ArrayList tll = tl[s.functionParam.Length - 1];

            for (int i = 0; i < FUNCTION_PARA_TYPE[s.function[s.function.Length - 1]].Length; i++)
            {
                switch (FUNCTION_PARA_TYPE[s.function[s.function.Length - 1]][i])
                {
                    case SByte:
                        tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        break;
                    case INT:
                        tll.Add(Int32.Parse(funPara[i].Text));
                        break;
                    case BOOLEAN:

                        tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        break;
                    case CHAR:
                        tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        break;
                    case SHORT:
                        tll.Add(Int16.Parse(funPara[i].Text));
                        break;
                    case LONG:
                        tll.Add(Int64.Parse(funPara[i].Text));
                        break;
                    case STRING:
                        tll.Add(funPara[i].Text);
                        break;
                }
            }
            return true;
        }

        public bool addcondition(int i1, int i2, int c, int ct, String cp)
        {
            Event s = subEvent[i1][i2];
            SByte[] newe;
            if (s.condition == null)
            {
                newe = new SByte[1];
                newe[0] = (SByte)c;
            }
            else
            {
                newe = new SByte[s.condition.Length + 1];
                for (int i = 0; i < s.condition.Length; i++)
                {
                    newe[i] = s.condition[i];
                }
                newe[s.condition.Length] = (SByte)c;
            }
            s.condition = newe;

            if (s.conditionType == null)
            {
                newe = new SByte[1];
                newe[0] = (SByte)ct;
            }
            else
            {
                newe = new SByte[s.conditionType.Length + 1];
                for (int i = 0; i < s.conditionType.Length; i++)
                {
                    newe[i] = s.conditionType[i];
                }
                newe[s.conditionType.Length] = (SByte)ct;
            }
            s.conditionType = newe;

            ArrayList[] tl;
            if (s.conditionParam == null)
            {
                tl = new ArrayList[1];
                tl[0] = new ArrayList();
            }
            else
            {
                tl = new ArrayList[s.conditionParam.Length + 1];
                for (int i = 0; i < s.conditionParam.Length; i++)
                {
                    tl[i] = s.conditionParam[i];
                }
                tl[s.conditionParam.Length] = new ArrayList();
            }
            s.conditionParam = tl;

            String[] ts = cp.Split(new Char[] { ',' });
            if (ts.Length < CONDITION_PARA_TYPE[s.condition[s.condition.Length - 1]].Length)
            {
                return false;

            }
            ArrayList tll = tl[s.conditionParam.Length - 1];

            for (int i = 0; i < CONDITION_PARA_TYPE[s.condition[s.condition.Length - 1]].Length; i++)
            {
                switch (CONDITION_PARA_TYPE[s.condition[s.condition.Length - 1]][i])
                {
                    case SByte:
                        tll.Add((SByte)Int16.Parse(ts[i]));
                        break;
                    case INT:
                        tll.Add(Int32.Parse(ts[i]));
                        break;
                    case BOOLEAN:

                        tll.Add((SByte)Int16.Parse(ts[i]));
                        break;
                    case CHAR:
                       tll.Add((SByte)Int16.Parse(ts[i]));
                        break;
                    case SHORT:
                        tll.Add(Int16.Parse(ts[i]));
                        break;
                    case LONG:
                        tll.Add(Int64.Parse(ts[i]));
                        break;
                    case STRING:
                        tll.Add(ts[i]);
                        break;
                }
            }
            return true;
        }

        public bool delcondition(int i1, int i2, int i3)
        {
            Event s = subEvent[i1][i2];
            if (i3 >= s.condition.Length)
            {
                return false;
            }
            SByte[] newc = new SByte[s.condition.Length - 1];                     
            int j = 0;
            for (int i = 0; i < s.condition.Length; i++)
            {
                if (i != i3)
                {
                    newc[j] = s.condition[i];
                    j++;
                }
            }

            s.condition = newc;

            newc = new SByte[s.conditionType.Length - 1];
            j = 0;
            for (int i = 0; i < s.conditionType.Length; i++)
            {
                if (i != i3)
                {
                    newc[j] = s.conditionType[i];
                    j++;
                }
            }

            s.conditionType = newc;

            ArrayList[] newl = new ArrayList[s.conditionParam.Length - 1];
            j = 0;
            for (int i = 0; i < s.conditionParam.Length; i++)
            {
                if (i != i3)
                {
                    newl[j] = s.conditionParam[i];
                    j++;
                }
            }

            s.conditionParam = newl;
            return true;
        }
        public bool delfunction(int i1, int i2, int i3)
        {
            Event s = subEvent[i1][i2];
            if (i3 >= s.function.Length)
            {
                return false;
            }
            SByte[] newc = new SByte[s.function.Length - 1];
            int j = 0;
            for (int i = 0; i < s.function.Length; i++)
            {
                if (i != i3)
                {
                    newc[j] = s.function[i];
                    j++;
                }
            }

            s.function = newc;
            

            ArrayList[] newl = new ArrayList[s.functionParam.Length - 1];
            j = 0;
            for (int i = 0; i < s.functionParam.Length; i++)
            {
                if (i != i3)
                {
                    newl[j] = s.functionParam[i];
                    j++;
                }
            }

            s.functionParam = newl;
            return true;
        }

        public bool upcondition(int i1, int i2, int i3, int c, int ct, String cp)
        {
            Event s = subEvent[i1][i2];

            if (i3 >= s.condition.Length)
            {
                return false;
            }
            s.condition[i3] = (SByte)c;



            s.conditionType[i3] = (SByte)ct;



            ArrayList tll = s.conditionParam[i3];
            String[] ts = cp.Split(new Char[] { ',' });
            if (ts.Length < CONDITION_PARA_TYPE[s.condition[i3]].Length)
            {
                return false;

            }
            if (tll.Count > CONDITION_PARA_TYPE[s.condition[i3]].Length)
            {
                tll.RemoveRange(CONDITION_PARA_TYPE[s.condition[i3]].Length, tll.Count - CONDITION_PARA_TYPE[s.condition[i3]].Length);
            }
            for (int i = 0; i < CONDITION_PARA_TYPE[s.condition[i3]].Length; i++)
            {
                switch (CONDITION_PARA_TYPE[s.condition[i3]][i])
                {
                    case SByte:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(ts[i]);
                        }
                        break;
                    case INT:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int32.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = Int32.Parse(ts[i]);
                        }
                        break;
                    case BOOLEAN:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(ts[i]);
                        }
                        break;
                    case CHAR:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(ts[i]);
                        }
                        break;
                    case SHORT:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int16.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = Int16.Parse(ts[i]);
                        }
                        break;
                    case LONG:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int64.Parse(ts[i]));
                        }
                        else
                        {
                            tll[i] = Int64.Parse(ts[i]);
                        }
                        break;
                    case STRING:
                        if (i >= tll.Count)
                        {
                            tll.Add(ts[i]);
                        }
                        else
                        {
                            tll[i] = ts[i];
                        }
                        break;
                }
            }
            return true;
        }
        public bool upfunction(int i1, int i2, int i3, int c)
        {
            Event s = subEvent[i1][i2];
            if (i3 >= s.function.Length)
            {
                return false;
            }
            s.function[i3] = (SByte)c;

            ArrayList tll = s.functionParam[i3];
           
            if (tll.Count > FUNCTION_PARA_TYPE[s.function[i3]].Length)
            {
                tll.RemoveRange(FUNCTION_PARA_TYPE[s.function[i3]].Length, tll.Count - FUNCTION_PARA_TYPE[s.function[i3]].Length);
            }
            for (int i = 0; i < FUNCTION_PARA_TYPE[s.function[i3]].Length; i++)
            {
                switch (FUNCTION_PARA_TYPE[s.function[i3]][i])
                {
                    case SByte:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(funPara[i].Text);
                        }
                        break;
                    case INT:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int32.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = Int32.Parse(funPara[i].Text);
                        }
                        break;
                    case BOOLEAN:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(funPara[i].Text);
                        }
                        break;
                    case CHAR:
                        if (i >= tll.Count)
                        {
                            tll.Add((SByte)Int16.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = (SByte)Int16.Parse(funPara[i].Text);
                        }
                        break;
                    case SHORT:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int16.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = Int16.Parse(funPara[i].Text);
                        }
                        break;
                    case LONG:
                        if (i >= tll.Count)
                        {
                            tll.Add(Int64.Parse(funPara[i].Text));
                        }
                        else
                        {
                            tll[i] = Int64.Parse(funPara[i].Text);
                        }
                        break;
                    case STRING:
                        if (i >= tll.Count)
                        {
                            tll.Add(funPara[i].Text);
                        }
                        else
                        {
                            tll[i] = funPara[i].Text;
                        }
                        break;
                }
            }
            return true;
        }
        public SByte getindex(String[] s, String s1)
        {
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i].Equals(s1))
                    return (SByte)i;
            }
            return -1;
        }
        public bool toevent(int i, int j)
        {
            bool result = false;
            Event s = toevent(form.textBox13.Lines);
            if (s != null)
            {
                result = true;
                subEvent[i][j] = s;
            }
            return result;

            
        }
        public Event toevent(String[] s)
        {
            Event e =  new Event(null);
            int count = 1;
            if (s != null && s.Length > 0)
            {
                try {
                    String[] ts = s[0].Split(new Char[] { ':' });
                    int len = Int32.Parse(ts[1]);
                    e.condition = new sbyte[len];
                    e.conditionParam = new ArrayList[len];
                    e.conditionType = new sbyte[len];
                    for (int i = 0; i < len; i++)
                    {
                        ts = s[count].Split(new Char[] { ':' });
                        e.condition[i] = getindex(CONDITION_STR, ts[1]);
                        ArrayList tll = new ArrayList();
                        e.conditionParam[i] = tll ;
                        for (int j = 0; j < CONDITION_PARA_TYPE[e.condition[i]].Length; j++)
                        {
                            String temps = ts[j + 3];
                            switch (CONDITION_PARA_TYPE[e.condition[i]][j])
                            {
                                case SByte:
                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case INT:
                                    tll.Add(Int32.Parse(temps));
                                    break;
                                case BOOLEAN:

                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case CHAR:
                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case SHORT:
                                    tll.Add(Int16.Parse(temps));
                                    break;
                                case LONG:
                                    tll.Add(Int64.Parse(temps));
                                    break;
                                case STRING:
                                    tll.Add(ts[j]);
                                    break;
                            }
                        }

                        e.conditionType[i] = getindex(TYPE_STR, ts[ts.Length-1]);
                        count++;
                    }


                    ts = s[count].Split(new Char[] { ':' });
                    len = Int32.Parse(ts[1]);
                    e.function = new sbyte[len];
                    e.functionParam = new ArrayList[len];
                    count++;
                    for (int i = 0; i < len; i++)
                    {
                        ts = s[count].Split(new Char[] { ':' });
                        e.function[i] = getindex(FUNCTION_STR, ts[1]);
                        ArrayList tll = new ArrayList();
                        e.functionParam[i] = tll;
                        for (int j = 0; j < FUNCTION_PARA_TYPE[e.function[i]].Length; j++)
                        {
                            String temps = ts[j + 3];
                            switch (FUNCTION_PARA_TYPE[e.function[i]][j])
                            {
                                case SByte:
                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case INT:
                                    tll.Add(Int32.Parse(temps));
                                    break;
                                case BOOLEAN:

                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case CHAR:
                                    tll.Add((SByte)Int16.Parse(temps));
                                    break;
                                case SHORT:
                                    tll.Add(Int16.Parse(temps));
                                    break;
                                case LONG:
                                    tll.Add(Int64.Parse(temps));
                                    break;
                                case STRING:
                                    tll.Add(temps);
                                    break;
                            }
                        }

                        count++;
                    }
                }
                catch (Exception e1)
                {
                    return null;
                }
            }
            return e;
        }
        public String[] toString()
        {
            String[] r = new String[200];
            int count = 1;
            if (condition != null)
            {
                r[0] = "条件:" + this.condition.Length;
                for (int i = 0; i < condition.Length; i++)
                {
                    r[count] = "条件" + i + ":" + CONDITION_STR[condition[i]];
                    r[count] = r[count] + ":参数值:";
                    for (int j = 0; j < conditionParam[i].Count; j++)
                    {
                        if (j == 0)
                        {
                            r[count] = r[count] + conditionParam[i][j];
                        }else
                        r[count] = r[count] + ":" + conditionParam[i][j];
                    }
                    if (conditionType[i] >= 3 || conditionType[i] < 0)
                    {
                        conditionType[i] = 0;
                    }
                    r[count] = r[count] + ":条件类型:" + TYPE_STR[conditionType[i]];
                    count++;
                }
            }
            if (function != null)
            {
                r[count] = "函数:" + function.Length+":个";
                count++;
                for (int i = 0; i < function.Length; i++)
                {
                    r[count] = "函数" + i + ":" + FUNCTION_STR[function[i]];
                    r[count] = r[count] + ":参数值:";
                    for (int j = 0; j < functionParam[i].Count; j++)
                    {
                        if (j == 0)
                        {
                            r[count] = r[count]  + functionParam[i][j];
                        }else
                        r[count] = r[count] + ":" + functionParam[i][j];
                    }
                    count++;
                }
            }
            return r;
        }
    }
}
